/**	
 * <br>
 * Copyright 2011 Chuan. All rights reserved.<br>
 * <br>			 
 * Package: com.chuan.mantu.abstracts <br>
 * FileName: PicGesture.java <br>
 * <br>
 * @version
 * @author javacyc@qq.com
 * @created 2012-11-16
 * @last Modified 
 * @history
 */

package com.chuan.mantu.abstracts;

import com.chuan.mantu.R;
import com.chuan.mantu.extend.PicShowAsyncTask;
import com.chuan.mantu.utils.Const;
import com.chuan.mantu.utils.ShareObj;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
import android.widget.ProgressBar;
import android.widget.ViewFlipper;

/**
 * {图片手势抽象类}
 *  
 *  @author javacyc@qq.com
 *  @created 2012-11-16 上午10:45:52
 *  @lastModified       
 *  @history           
 */

public abstract class PicGesture extends Activity implements OnGestureListener{
	
	/**
	 * 手势开关，当处于loading状态时，开关关闭
	 */
	private boolean PIC_GESTURE_SWITCH = false;
	
	private ViewFlipper flipper;

	private ProgressBar progressBar;
	
	private GestureDetector detector;
	
	/**
	 * 
	 *  ｛初始化相关参数｝
	 *  @param flipper
	 *  @author javacyc@qq.com
	 *  @created 2012-11-16 上午10:53:36
	 *  @lastModified       
	 *  @history
	 */
	public void initParams(ViewFlipper flipper, ProgressBar progressBar) {
		this.flipper = flipper;
		this.progressBar = progressBar;
	}

	/**
	 * 
	 *  ｛运行加载图片任务｝
	 *  @author javacyc@qq.com
	 *  @created 2013-2-27 下午04:10:44
	 *  @lastModified       
	 *  @history
	 */
	public void runTask(){
		PicShowAsyncTask picShowAsyncTask = new PicShowAsyncTask(this, flipper, getIntent().getStringExtra(Const.PIC_CATEGORY), Const.GESTURE_DEFAULT);
		picShowAsyncTask.execute("");
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		detector = new GestureDetector((OnGestureListener) this);
	}

	public void offProgressBar(){
		progressBar.setVisibility(View.GONE);
		flipper.setVisibility(View.VISIBLE);
	}
	
	public void onProgressBar(){
		progressBar.setVisibility(View.VISIBLE);
		flipper.setVisibility(View.GONE);
	}
	
	public void setPIC_GESTURE_SWITCH(boolean pIC_GESTURE_SWITCH) {
		PIC_GESTURE_SWITCH = pIC_GESTURE_SWITCH;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}
	
	@Override
	public boolean onDown(MotionEvent e) {
		return true;
	}
	
	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,	float velocityY) {
		if(PIC_GESTURE_SWITCH){
			String category = getIntent().getStringExtra(Const.PIC_CATEGORY);
			if (e1.getX() - e2.getX() > 120) {//  手势如图：<
				if((flipper.getDisplayedChild() + 1) == flipper.getChildCount()){
					return false;
				}
				flipper.setInAnimation(AnimationUtils.loadAnimation(this,	R.anim.push_left_in));
				flipper.setOutAnimation(AnimationUtils.loadAnimation(this,	R.anim.push_left_out));
				flipper.showNext();
				ShareObj.INSTANCE.setCURRENTPICNAME(category
						,ShareObj.INSTANCE.getPicMap().get(category).get(ShareObj.INSTANCE.getCategoryIndex(category)).get(flipper.getDisplayedChild()).getName());
				return true;
			} else if (e1.getX() - e2.getX() < -120) {//  手势如图：>
				if(flipper.getDisplayedChild() == 0){
					return false;
				}
				flipper.setInAnimation(AnimationUtils.loadAnimation(this,	R.anim.push_right_in));
				flipper.setOutAnimation(AnimationUtils.loadAnimation(this,	R.anim.push_right_out));
				flipper.showPrevious();
				ShareObj.INSTANCE.setCURRENTPICNAME(category
						,ShareObj.INSTANCE.getPicMap().get(category).get(ShareObj.INSTANCE.getCategoryIndex(category)).get(flipper.getDisplayedChild()).getName());
				return true;
			}
			
			if (e1.getY() - e2.getY() > 120) {//  手势如图：∧
				onProgressBar();
				setPIC_GESTURE_SWITCH(false);
				PicShowAsyncTask picShowAsyncTask = new PicShowAsyncTask(this, flipper, category, Const.GESTURE_UP);
				picShowAsyncTask.execute("");
				return true;
			} else if (e1.getY() - e2.getY() < -120) { //  手势如图：∨
				onProgressBar();
				setPIC_GESTURE_SWITCH(false);
				PicShowAsyncTask picShowAsyncTask = new PicShowAsyncTask(this, flipper, category, Const.GESTURE_DOWN);
				picShowAsyncTask.execute("");
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}


	@Override
	public void onShowPress(MotionEvent e) {
	}

	/**
	 *  单击弹出全屏
	 *  @param e
	 *  @return
	 *  @author javacyc@qq.com
	 *  @created 2012-11-16 下午7:47:28
	 *  @lastModified      
	 *  @history
	 */
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}
}